/**
 * 
 */
package com.wistech.search.solr.extend.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.wistech.commons.dao.hibernate.HibernateDao;
import com.wistech.search.solr.extend.model.Company;

/**
 * @author Liuye
 * 
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Repository
public class CompanyDao extends HibernateDao<Company, Long> {
	private static final String QUERY_USER_ID_BY_ONLINE_SQL = "SELECT user_id FROM company WHERE online=?";
	private static final String QUERY_SCORE = "SELECT score FROM company WHERE id=?";

	public List<Long> findCompanyUserIdsByOnline(final boolean online) {
		return (List<Long>) getHibernateTemplate().executeWithNativeSession(
				new HibernateCallback() {
					public Object doInHibernate(final Session session)
							throws HibernateException {
						final Query q = session.createSQLQuery(
								QUERY_USER_ID_BY_ONLINE_SQL).addScalar(
								"user_id", StandardBasicTypes.LONG);
						q.setParameter(0, online);
						return q.list();
					}
				});
	}

	public Float getScore(final Long id) {
		return (Float) getHibernateTemplate().executeWithNativeSession(
				new HibernateCallback() {
					public Object doInHibernate(final Session session)
							throws HibernateException {
						final Query q = session.createSQLQuery(QUERY_SCORE)
								.addScalar("id", StandardBasicTypes.FLOAT);
						q.setParameter(0, id);
						return q.uniqueResult();
					}
				});
	}
}
